﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using FetchDataSystem.DAL;

namespace FetchDataSystem
{
    public partial class FrmLogin : System.Windows.Forms.Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

      


        private void BtnLogin_Click(object sender, EventArgs e)
        {
            if (!IsValidate())
            {
                return;
            }
            //1.验证用户
            bool flag = UserService.CheckUser(this.txtUserName.Text.Trim());
            if (!flag)
            {
                MessageBox.Show("该用户不存在！", "提示");
                return;
            }
            //2.验证登录
           string userId= UserService.CheckLogin(this.txtUserName.Text.Trim(), this.txtPwd.Text.Trim());
            if (userId==null)
            {
                MessageBox.Show("密码错误！", "提示");
                return;
            }

            //3.保存信息，跳转到主窗体
            Session.UserId = userId;
            Session.LoginName = this.txtUserName.Text.Trim();
            Session.LoginPwd = this.txtPwd.Text.Trim();
            //跳转窗体
            FrmMain frmMain = new FrmMain();
            frmMain.Show();
            this.Hide();

        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //非空验证
        private bool IsValidate()
        {
            if (String.IsNullOrWhiteSpace(this.txtUserName.Text)){
                MessageBox.Show("用户名不能为空！");
                return false;
            }
            if (String.IsNullOrWhiteSpace(this.txtPwd.Text))
            {
                MessageBox.Show("密码不能为空!");
                return false;
            }
            return true;
        }


    }
}
